package com.dream.xcarrecorder;

import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.os.Build;
import android.os.IBinder;

import androidx.annotation.Nullable;

import com.dream.xcarrecorder.rec.RecHelper;

public class RecService extends Service {
    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @Override
    public void onCreate() {
        super.onCreate();
        onNotification();

        try {
            RecHelper.getInstance().init(this);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void start(Context ct){
        Intent i = new Intent(ct,RecService.class);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            ct.startForegroundService(i);
        }else{
            ct.startService(i);
        }
    }

    private void onNotification(){
        String CHANNEL_ONE_ID = "com.dream.xcarrecorder.service.id";
        String CHANNEL_ONE_NAME = "com.dream.xcarrecorder.service.name";

        Notification notification = null;

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            NotificationChannel notificationChannel = new NotificationChannel(CHANNEL_ONE_ID,
                    CHANNEL_ONE_NAME, NotificationManager.IMPORTANCE_HIGH);
            notificationChannel.enableLights(true);//是否点亮提示灯
            notificationChannel.setLightColor(Color.RED);//提示灯颜色
            notificationChannel.setShowBadge(true);//角标
            notificationChannel.setLockscreenVisibility(Notification.VISIBILITY_PUBLIC);//锁屏是否可见
            NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
            manager.createNotificationChannel(notificationChannel);

            notification = new Notification.Builder(this,CHANNEL_ONE_ID)
                    .setContentTitle("录像服务")
                    .setContentText("录像服务正在运行中...")
                    .setSmallIcon(R.mipmap.ic_launcher)
                    .build();
        }else{
            notification = new Notification.Builder(this)
                    .setContentTitle("录像服务")
                    .setContentText("录像服务正在运行中...")
                    .setSmallIcon(R.mipmap.ic_launcher)
                    .build();
        }

        notification.flags |= Notification.FLAG_NO_CLEAR;
        startForeground(1, notification);
    }


}
